package org.example.serviceEdu.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.ResultCode.R;
import org.example.exceptionHandler.GuliException;
import org.example.serviceEdu.client.VodClient;
import org.example.serviceEdu.entity.EduVideo;
import org.example.serviceEdu.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lining
 * @since 2023-10-25
 */
@CrossOrigin
@RestController
@RequestMapping("/serviceEdu/video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;
    //注入vodClient
    @Autowired
    private VodClient vodClient;

    //    添加
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return  R.ok();
    }
//    删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        EduVideo eduVideo=videoService.getById(id);
        String videoSourceId=eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "根据单个视频id删除视频失败,熔断器...");//熔断测试
            }

        }

        videoService.removeById(id);
        return R.ok();
    }
//    根据id查询
    @GetMapping("getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable String videoId){
        EduVideo eduVideo=videoService.getById(videoId);
        return R.ok().data("video",eduVideo);
    }
//    修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }

}

